package assembler

import (
	"ginfwk/application/dto"
	"ginfwk/domain/model/entity"
	"time"
)

type UserAssembler struct {
}

func (s *UserAssembler) ToUserEntity(userDTO *dto.UserDTO) *entity.UserEntity {
	if userDTO == nil {
		return nil
	}
	return &entity.UserEntity{
		Id:        userDTO.Id,
		Name:      userDTO.Name,
		Age:       userDTO.Age,
		Address:   userDTO.Address,
		CreatedAt: time.Now(),
		UpdatedAt: time.Now(),
	}
}

func (s *UserAssembler) ToUserDTO(user *entity.UserEntity) *dto.UserDTO {
	if user == nil {
		return nil
	}
	userDTO := &dto.UserDTO{
		Id:      user.Id,
		Name:    user.Name,
		Age:     user.Age,
		Address: user.Address,
	}
	return userDTO
}

func (s *UserAssembler) ToUserDTOList(users []*entity.UserEntity) []*dto.UserDTO {
	userList := []*dto.UserDTO{}
	if users == nil || len(users) == 0 {
		return userList
	}
	for _, u := range users {
		if userItem := s.ToUserDTO(u); userItem != nil {
			userList = append(userList, userItem)
		}
	}
	return userList
}
